package com.mkyong.editor;

import java.util.HashSet;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import com.sunrise.xdoc.entity.user.Role;
import com.sunrise.xdoc.entity.user.User;
import com.sunrise.xdoc.helper.DaoHelper;
import com.sunrise.xdoc.service.IUserService;

@ManagedBean(name = "navigator")
@SessionScoped
public class Navigator {
	private String pageToNavigate = "";

	@ManagedProperty(value = "#{userService}")
	private IUserService userService;

	@ManagedProperty(value = "#{daoHelper}")
	private DaoHelper daoHelper;

	public DaoHelper getDaoHelper() {
		return daoHelper;
	}

	public void setDaoHelper(DaoHelper daoHelper) {
		this.daoHelper = daoHelper;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public String navigateTo() {
		User u = new User();
		u.setUsername("php");
		u.setPassword("123456");
		u.setCode("userCode");

		Role r = new Role(Role.ADMIN);
		r.setCode("roleCode");
		Set<Role> roles = new HashSet<>();
		roles.add(r);

		u.setRoles(roles);
		System.out.println("-----------" + userService.getClass());
		getDaoHelper().getDao(User.class).saveOrUpdate(u);

		if ("ToSecure".equalsIgnoreCase(pageToNavigate)) {
			return "Secured";
		} else if ("ToUnSecure".equalsIgnoreCase(pageToNavigate)) {
			return "UnSecured";
		} else {
			// This will never happen but we will use this to extend this
			// application
			return "none";
		}
	}

	public String getPageToNavigate() {
		return pageToNavigate;
	}

	public void setPageToNavigate(String option) {
		this.pageToNavigate = option;
	}
}